package name.jautzy.esi.client;

import java.util.ArrayList;
import java.util.List;

import name.jautzy.esi.client.ressources.StandardRessources;

import name.jautzy.esi.client.model.*;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.BaseTreeLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.data.TreeLoader;
import com.google.gwt.user.client.rpc.AsyncCallback; 
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.button.ToggleButton;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGrid;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGridCellRenderer;

public class DisplayDbListener implements Listener<ButtonEvent> {
private ContentPanel parent = null;
private ContentPanel center = null;
	
	public DisplayDbListener (ContentPanel parent) {
		this.parent = parent;
	}
	
	public void handleEvent(ButtonEvent ce) {  
        if (!ce.<ToggleButton> getComponent().isPressed()) {  
          return;  
        }  
        
        center = new ContentPanel();
        
        ContentPanel panel = new ContentPanel();
        panel.setHeading("Eve Database");
        panel.setHeaderVisible(false);
        panel.setBodyStyle("background-color:black;"
				  +"background-image:url(ressources/images/background/01_1024.jpg);"
				  +"backgroung-repeat:no-repeat;");

        RpcProxy<List<EdbObject>> proxy = new RpcProxy<List<EdbObject>>() {  
          @Override  
          protected void load(Object loadConfig, AsyncCallback<List<EdbObject>> callback) { 
        	if (loadConfig==null)
        		Esi.getEsi().getDatabaseService().getObjects(null,callback);  
        	else
        		Esi.getEsi().getDatabaseService().getObjects((EdbObject)loadConfig,callback);  
          }  
        }; 
       
        TreeLoader<EdbObject> loader = new BaseTreeLoader<EdbObject>(proxy) {  
        	  @Override  
        	  public boolean hasChildren(EdbObject parent) {  
        	    return !(parent instanceof EdbEquipment);
        	  }  
        };  
        	
        TreeStore<EdbObject> store = new TreeStore<EdbObject>(loader); 

        List<ColumnConfig> columns = new ArrayList<ColumnConfig>();  
        ColumnConfig name = new ColumnConfig("name", "Name", 200);
        name.setRenderer(new TreeGridCellRenderer<ModelData>());
        columns.add(name);    
        ColumnModel cm = new ColumnModel(columns);
        
        TreeGrid<ModelData> tree = new TreeGrid<ModelData>(store, cm);
        tree.setAutoExpandColumn("name");
        tree.getStyle().setLeafIcon(StandardRessources.getIcon(StandardRessources.LTLICONS.leaf2()));      
        tree.addListener(Events.CellClick,new DisplayDbEquipmentListener(center));
        
        panel.setLayout(new BorderLayout());    
          
	    center.setHeaderVisible(false);  
	    center.setLayout(new FitLayout());  
	    center.add(new Html(  
	        "<p style=\"padding:10px;color:#556677;font-size:11px;\">Select an itemt</p>"));  
	    BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
	    centerData.setMargins(new Margins(5));
	    
	    ContentPanel west = new ContentPanel();  
	    west.setHeaderVisible(false);  
	    west.setLayout(new FitLayout()); 
	    BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 150, 100, 250);  
	    westData.setMargins(new Margins(5));  
	    westData.setSplit(true); 
	    westData.setCollapsible(true); 
	    west.add(tree);
	      
	    panel.add(center, centerData);
	    panel.add(west, westData); 
        
	    parent.removeAll();  
	    parent.add(panel);  
	    parent.layout();     
	}  
}
